package com.heima.media.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.SensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author SpaceTraveller
 * @since 2022-11-09
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {

    @Override
    public PageResponseResult getSensitiveList(SensitiveDto dto) {
        LambdaQueryWrapper<WmSensitive> query = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getName())) {
            query.like(WmSensitive::getSensitives, dto.getName());
        }
        Page<WmSensitive> iPage = new Page<>(dto.getPage(),dto.getSize());
        Page<WmSensitive> page = this.page(iPage, query);
        PageResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),page.getTotal(),page.getRecords());
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        return result;
    }

    @Override
    public ResponseResult putSensitives(Map<String,String> sensitives) {
        String sensitive = sensitives.get("sensitives");
        Date date = new Date();
        String[] split = sensitive.split(",");
        List<WmSensitive> sensitiveList = new ArrayList<>();
        for (String s : split) {
            WmSensitive wmSensitive = new WmSensitive();
            wmSensitive.setSensitives(s);
            wmSensitive.setCreatedTime(date);
            sensitiveList.add(wmSensitive);
        }
        this.saveBatch(sensitiveList);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateSensitives(WmSensitive sensitives) {
        sensitives.setCreatedTime(new Date());
        this.updateById(sensitives);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult testExcelPutIn(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), WmSensitive.class, new ReadListener<WmSensitive>() {
            @Override
            public void invoke(WmSensitive sensitive, AnalysisContext analysisContext) {
                save(sensitive);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            }
        }).sheet().doRead();
        return ResponseResult.okResult();
    }
}
